package com.aube;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.aube.activity.PluginMainActivity;

/**
 * Created by huyaonan on 16/11/9.
 */
public class StaticApplicationContext {
    public static Application application;
    public static Context context;

    public static String MI_PUSH_ID = "";
    public static String MI_PUSH_KEY = "";

    private static AubePushHandler mHandler = null;

    public static long RECEIVED_DATA_SIZE = 0;
    public static long SENDED_DATA_SIZE = 0;
    public static long receivedDataSize = 0;

    public static class AubePushHandler extends Handler {

        private Context context;

        public AubePushHandler(Context context) {
            this.context = context;
        }

        @Override
        public void handleMessage(Message msg) {
            if(msg.what == 1) {
                startActivity(context, (Bundle)msg.obj);
            } else {
                String s = (String) msg.obj;
                if (!TextUtils.isEmpty(s)) {
                    //Toast.makeText(context, s, Toast.LENGTH_LONG).show();
                }
            }
        }

        public void startActivity(Context context, Bundle bundle) {
            Intent aIntent = new Intent();
            aIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            aIntent.setClass(context, PluginMainActivity.class);
            aIntent.putExtras(bundle);
            context.startActivity(aIntent);
        }
    }

    public static void init(Application app) {
        application = app;
        context = app;

        // Setup push handler
        if (mHandler == null) {
            mHandler = new AubePushHandler(application);
        }
    }

    public static void sendMessage(Message message) {
        if(mHandler == null)
            return;
        mHandler.sendMessage(message);
    }

    public static Message obtainMessage() {
        if(mHandler == null)
            return new Message();
        return mHandler.obtainMessage();
    }

}
